package edu.ucsd.cse110;

import java.util.Calendar;
import java.util.Date;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import edu.ucsd.cse110.model.Lift;

/**
 * Adds a 1-rep, 3-rep, or 5-rep lift record to database.
 */
public class LiftEditButtonActivity extends Activity {
	private Context mContext;
	private EditText mEtOneRep, mEtThreeRep, mEtFiveRep;
	private int mLiftId;

	/**
	 * TODO: Need documentation.
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.lifts_edit);

		mContext = this;

		Bundle extras = getIntent().getExtras();
		mLiftId = extras.getInt("LiftId");

		mEtOneRep = (EditText) findViewById(R.id.liftsedit_et_rep1);
		mEtThreeRep = (EditText) findViewById(R.id.liftsedit_et_rep3);
		mEtFiveRep = (EditText) findViewById(R.id.liftsedit_et_rep5);

		TextView txtRepOne = (TextView) findViewById(R.id.liftsedit_txt_currentrep1);
		TextView txtRepThree = (TextView) findViewById(R.id.liftsedit_txt_currentrep3);
		TextView txtRepFive = (TextView) findViewById(R.id.liftsedit_txt_currentrep5);

		txtRepOne.setText(Integer.toString(extras.getInt("RepOne")));
		txtRepThree.setText(Integer.toString(extras.getInt("RepThree")));
		txtRepFive.setText(Integer.toString(extras.getInt("RepFive")));

		Button btnSave = (Button) findViewById(R.id.liftsedit_btn_submit);
		btnSave.setOnClickListener(new OnClickListener() {
			/**
			 * TODO: Need documentation.
			 */
			public void onClick(View v) {
				Dialog dialog = new Dialog(mContext);
				dialog.setContentView(R.layout.lifts_edit);
				dialog.setTitle("Edit Lift");
				dialog.setCancelable(true);

				Date date = Calendar.getInstance().getTime();
				Lift newLift = new Lift();
				newLift.setLiftId(mLiftId);

				if (!(mEtOneRep.getText().toString().equals(""))) {
					newLift.setRepOne(Integer.parseInt(mEtOneRep.getText()
							.toString()));
					newLift.setDateOne(date);
				}
				if (!(mEtThreeRep.getText().toString().equals(""))) {
					newLift.setRepThree(Integer.parseInt(mEtThreeRep.getText()
							.toString()));
					newLift.setDateThree(date);
				}
				if (!(mEtFiveRep.getText().toString().equals(""))) {
					newLift.setRepFive(Integer.parseInt(mEtFiveRep.getText()
							.toString()));
					newLift.setDateFive(date);
				}

				LocalDatabaseManager database = new LocalDatabaseManager(
						mContext);
				database.insertLift(newLift);
				database.close();

				Intent data = new Intent();
				data.putExtra("RepOne", newLift.getRepOne());
				data.putExtra("RepThree", newLift.getRepThree());
				data.putExtra("RepFive", newLift.getRepFive());
				data.putExtra("Date", date.getTime());
				setResult(RESULT_OK, data);
				finish();
			}
		});
	}
}